﻿using Base.Common.Events;
using log4net;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Input;

namespace Base.ModuleBase
{
    public class ViewModelBase : BindableBase, INavigationAware
    {
        #region 成员

        protected readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// View激活状态改变时发生
        /// </summary>        
        public event Action<bool> ActiveEvent;

        #endregion

        #region 属性

        private bool _isActive;
        /// <summary>
        /// 指示View是否激活
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                SetProperty(ref _isActive, value);
                ActiveEvent?.Invoke(value);
            }
        }

        #endregion

        #region 构造
        /// <summary>
        /// 初始化构造
        /// </summary>
        public ViewModelBase()
        {
            OnInit();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void OnInit()
        {
            GlobalManager.EventAggregator.GetEvent<LogoutEvent>().Subscribe((args) =>
            {
                AsyncActionHelper.RunDispatcherTask(() => OnLogout(args));
            });
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <param name="args"></param>
        public virtual void OnLogout(object args)
        {

        }

        /// <summary>
        /// 进入页面
        /// </summary>
        /// <param name="navigationContext"></param>
        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            IsActive = true;
        }

        /// <summary>
        /// 是否处理请求（false会创建新实例）
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <returns></returns>
        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        /// <summary>
        /// 离开页面
        /// </summary>
        /// <param name="navigationContext"></param>
        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {
            IsActive = false;
        }
        #endregion
    }
}
